﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using MongoDB.Driver;
using System.Configuration;
namespace sqltomongo
{
    public class MongoDB<T> where T:class,new()
    {
        public MongoDB()
        {}
        private string MongoUrl { get; } = ConfigurationManager.AppSettings.Get("mongo_conn");
        private string DbName { get;} = ConfigurationManager.AppSettings.Get("db_name");
        /// <summary>
        /// 得到数据库实例
        /// </summary>
        /// <returns></returns>
        private IMongoDatabase GetDataBase()
        {
            MongoClient client = new MongoClient(new MongoUrl(MongoUrl));
            return client.GetDatabase(DbName);
        }
        public IMongoCollection<T> GetCollection(string collectionName= "test")
        {
            var db = GetDataBase();
            if (null != db)
            {
                return db.GetCollection<T>(collectionName);
            }
            return null;
        }
    }
}